/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import ru.portnyagin.helpdeskru.model.StateRequest;
import ru.portnyagin.helpdeskru.service.StateRequestService;
import ru.portnyagin.helpdeskru.util.EJB;

    
@FacesConverter(forClass = StateRequest.class)
public class StateRequestConverter implements Converter {
    
    private StateRequestService stateRequestService = EJB.lookup(StateRequestService.class);
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }

        StateRequest dicSost = stateRequestService.find(value);

        if (dicSost == null) {
            throw new ConverterException(new FacesMessage("Unknown StateRequest ID: " + value));
        }

        return dicSost;
    }
    
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (!(value instanceof StateRequest) || ((StateRequest) value).getId() == null) {
            return null;
        }

        return ((StateRequest) value).getId();
    }
    
    
    
    
    
}
